#!/usr/bin/env python3
"""
简单的API测试脚本
"""

import requests
import json

def test_api():
    base_url = "http://127.0.0.1:8001"
    
    # 测试根端点
    try:
        response = requests.get(f"{base_url}/")
        print(f"根端点状态码: {response.status_code}")
        if response.status_code == 200:
            print("✅ 后端API根端点正常")
            print(f"响应内容: {response.json()}")
        else:
            print("❌ 后端API根端点异常")
            return False
    except Exception as e:
        print(f"❌ 连接后端API失败: {str(e)}")
        return False
    
    # 测试聊天API端点
    try:
        response = requests.post(
            f"{base_url}/v1/chat/",
            json={
                "message": "你好，这是一个测试消息",
                "session_id": "test_session_123",
                "stream": False
            }
        )
        print(f"聊天API状态码: {response.status_code}")
        if response.status_code == 200:
            print("✅ 聊天API正常工作")
            response_json = response.json()
            print(f"响应内容: {json.dumps(response_json, ensure_ascii=False, indent=2)}")
        else:
            print("❌ 聊天API响应异常")
            print(f"错误内容: {response.text}")
    except Exception as e:
        print(f"❌ 调用聊天API失败: {str(e)}")
    
    return True

if __name__ == "__main__":
    print("开始测试后端API...")
    test_api()
    print("测试完成")